/*
 * ADD.c
 *
 *  Created on: Jan 18, 2014
 *      Author: adijoshi
 */
//INCLUDES

#include "includes.h"

/*
 * Function: func_ADD
 *
 * Inputs:	 Mnemonic, Address_of_source, Address_of_destination
 * Outputs:  Result
 * Condition  Codes:
 * 		Z:  set  if  result  =  0;  cleared  otherwise
 * 		N:  set  if  result  <  0;  cleared  otherwise
 * 		C:  set  if  there  was  a  carry  from  the  most  significant bit  of  the  result;  cleared  otherwise
 * 		V:  set  if  there  was  arithmetic  overflow;  that  is,  if  both  operands  were  of  the  same
 * 			sign  and  the  result  was  of  the  opposite  sign; ;  cleared otherwise.
 */

unsigned short func_ADD(unsigned short address_source, unsigned short address_destination)
{
	unsigned short data_source;
	unsigned short data_destination;
	int result_32_bit;
	unsigned short result;

	data_source 	 = read_from_memory(address_source,DATA);
	data_destination = read_from_memory(address_destination,DATA);

	result_32_bit = data_source + data_destination;
	result		  = (unsigned short)result_32_bit;
	write_in_memory(address_destination, result,DATA);

	update_Z_flag(result);
	update_V_flag(ADD, data_source, data_destination, result);
	update_N_flag(ADD, result);
	update_C_flag(ADD, data_source, data_destination, result, result_32_bit);

	return result;
}



